<template>
  <el-dialog
    title="新增分支"
    :close-on-click-modal="false"
    width="50%"
    :visible.sync="visible">

    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"
             label-width="80px">

      <el-row>
        <el-col :span="20">
          <el-form-item label="分支编码" label-width="120px" >
            <el-input v-model="dataForm.branchCode" disabled placeholder="分支编码"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item label="分支名称" label-width="120px" prop="branchName">
            <el-input v-model="dataForm.branchName" placeholder="分支名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item label="分支描述" label-width="120px" prop="">
            <el-input type="textarea" :rows="3" v-model="dataForm.branchDesc" placeholder="规则组描述"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>


    <span slot="footer" class="dialog-footer">
      <el-button type="info" plain @click="dataFormSubmit()">确定</el-button>
      <el-button @click="visible = false">返回</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {saveData, getData} from '@/api/ruleConfig/branchInfo'
    export default {
        data() {
            return {

                visible: false,
                dataForm: {
                    id: null,
                    branchCode: '',
                    branchName: '',
                    branchDesc: '',
                    branchType:'2',
                    remark: '',
                    isEffect: '1'
                },

                dataRule: {
                    branchName: [
                        {required: true, message: '分支名称不能为空', trigger: 'change'}
                    ]
                }

            }
        },
        created() {

        },
        methods: {

            //初始化
            init(id) {
                this.visible = true

                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (id) {
                        this.dataForm.id = id
                        let params = {
                            id: this.dataForm.id + ''
                        }
                        this.$store.dispatch('loading/CHANGE_LOADING', true)
                        getData(params).then(res => {
                            this.$store.dispatch('loading/CHANGE_LOADING', false)
                            if (res.data.data && res.data.resultCode === 0) {
                                this.dataForm = res.data.data
                            }
                        }).catch(err=>{
                            this.$store.dispatch('loading/CHANGE_LOADING', false)
                        })
                    }
                })
            },


            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$store.dispatch('loading/CHANGE_LOADING', true)
                        saveData(JSON.stringify(this.dataForm)).then(res => {
                            this.$store.dispatch('loading/CHANGE_LOADING', false)
                            if (res.data && res.data.resultCode === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(res.data.resultMsg)
                            }
                        }).catch(err=>{
                            this.$store.dispatch('loading/CHANGE_LOADING', false)
                        })
                    }
                })
            }
        }
    }
</script>

<style lang="scss">
  .title {
    padding: 10px;
    background-color: rgba(248, 248, 248, 1);
    margin-bottom: 10px;
  }

  .el-table__row .el-form-item__content {
    margin-left: 0 !important;
  }

  .el-table__row .el-form-item {
    margin-bottom: 0 !important;
  }
</style>
